﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Data;

namespace Bll
{
    class CHelpManage
    {
        public static CHelp CreateHelp()
        {
            return new CHelp();
        }

        public static CHelp Save(CHelp help)
        {
            try
            {
                int num = BLL.SqlHelper.ExecuteNonQuery(CConnection.getConnString(), "sp_AddHelp", help.ID, help.Title, help.Content );

                if (0 == num)
                    return null;
                return help;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public static bool Update(CHelp help)
        {
            try
            {
                int num = BLL.SqlHelper.ExecuteNonQuery(CConnection.getConnString(), "sp_SaveHelp", help.ID, help.Title, help.Content);
                if (0 == num)
                    return false;
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public static bool Delete(CHelp help)
        {
            try
            {
                int num = BLL.SqlHelper.ExecuteNonQuery(CConnection.getConnString(), "sp_RemoveHelp", help.ID);
                if (0 == num)
                    return false;
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public static CHelp GetHelp(String ID)
        {
            SqlDataReader reader = BLL.SqlHelper.ExecuteReader(CConnection.getConnString(), "sp_GetHelp", ID);
            try
            {

                
                if (reader.Read())
                {
                    CHelp help = new CHelp();
                    help.Title = reader["Title"].ToString();
                    help.Content = reader["Content"].ToString();
   
                    return help;
                }
                return null;
            }
            catch (Exception e)
            {

                //reader.Close();
                return null;
            }
            finally
            {
                reader.Close();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static DataTable LoadAllHelp()
        {
            SqlDataReader reader = BLL.SqlHelper.ExecuteReader(CConnection.getConnString(), "sp_GetHelpByCondition");

            var dataTable = new DataTable();

            dataTable.Columns.Add("ID");
            dataTable.Columns.Add("Title");
            dataTable.Columns.Add("Content");

            try
            {
                while (reader.Read())
                {
                    dataTable.Rows.Add(reader["ID"].ToString(),reader["Title"].ToString(), reader["Content"].ToString()); 
                }
                return dataTable;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                reader.Close();
            }
            
        }
     
    }
}